package com.qf.wangqing.sportsnews.fragment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qf.wangqing.sportsnews.MyApplication;
import com.qf.wangqing.sportsnews.R;
import com.qf.wangqing.sportsnews.activity.MainActivity;
import com.qf.wangqing.sportsnews.adapter.NewsPagerAdapter;
import com.qf.wangqing.sportsnews.bean.newsbean.NewsTitleEntity;
import com.qf.wangqing.sportsnews.callback.MainBindingInterface;
import com.qf.wangqing.sportsnews.databinding.ActivityMainBinding;
import com.qf.wangqing.sportsnews.databinding.FragmentNewsBinding;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * A simple {@link Fragment} subclass.
 */
public class NewsFragment extends Fragment implements Callback<NewsTitleEntity>, View.OnClickListener {


    private FragmentNewsBinding dataBinding;
    private ActivityMainBinding mainBinding ;
    private MainBindingInterface listener;

    public static NewsFragment newInstance(MainBindingInterface listener) {
        
        Bundle args = new Bundle();
        NewsFragment fragment = new NewsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_news, container, false);
      
        return dataBinding.getRoot();
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        MyApplication application = (MyApplication) getActivity().getApplication();
        application.getNewsService().getChannels().enqueue(this);

        //slidingpane
        dataBinding.imageHead.setOnClickListener(this);

    }

    @Override
    public void onResponse(Call<NewsTitleEntity> call, Response<NewsTitleEntity> response) {
        NewsTitleEntity body = response.body();
        if (body.getData().getChannels() != null ) {
            NewsPagerAdapter newsPagerAdapter = new NewsPagerAdapter(getActivity().getSupportFragmentManager(), body.getData().getChannels());
            dataBinding.viewpagerNews.setAdapter(newsPagerAdapter);
            dataBinding.tablayoutNewsfragment.setupWithViewPager(dataBinding.viewpagerNews);
        }
    }

    @Override
    public void onFailure(Call<NewsTitleEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        MainActivity activity = (MainActivity) getActivity();
        mainBinding = activity.getBinding();
        if (mainBinding.slidingMain.isOpen()) {
            mainBinding.slidingMain.closePane();
        }else {
            mainBinding.slidingMain.openPane();
        }

        }

}
